<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Variable scopes</title>
<meta name="generator" content="DocBook XSL Stylesheets V1.75.2">
<link rel="home" href="index.html" title="Smarty 3 Manual">
<link rel="up" href="language.variables.html" title="Chapter 4. Variables">
<link rel="prev" href="language.variables.html" title="Chapter 4. Variables">
<link rel="next" href="language.config.variables.html" title="Variables loaded from config files">
</head>
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
<div class="navheader">
<table width="100%" summary="Navigation header">
<tr><th colspan="3" align="center">Variable scopes</th></tr>
<tr>
<td width="20%" align="left">
<a accesskey="p" href="language.variables.html">Prev</a> </td>
<th width="60%" align="center">Chapter 4. Variables</th>
<td width="20%" align="right"> <a accesskey="n" href="language.config.variables.html">Next</a>
</td>
</tr>
</table>
<hr>
</div>
<div class="sect1" title="Variable scopes">
<div class="titlepage"><div><div><h2 class="title" style="clear: both">
<a name="language.variable.scopes"></a>Variable scopes</h2></div></div></div>
<p>
    You have the choice to assign variables to the scope of the main Smarty object, 
    data objects created with <a class="link" href="api.create.data.html" title="createData()"><code class="varname">createData()</code></a>,
    and template objects created with <a class="link" href="api.create.template.html" title="createTemplate()"><code class="varname">createTemplate()</code></a>. 
    These objects can be chained. A template sees all the variables of its own object and all variables assigned 
    to the objects in its chain of parent objects. 
   </p>
<p>
    By default templates which are rendered by <a class="link" href="api.display.html" title="display()"><code class="varname">$smarty-&gt;display(...)</code></a>
    or <a class="link" href="api.fetch.html" title="fetch()"><code class="varname">$smarty-&gt;fetch(...)</code></a> calls are automatically linked to the
    Smarty object variable scope.
   </p>
<p>
    By assigning variables to individual data or template objects you have full control which variables can be seen
    by a template.
   </p>
<p>
    </p>
<div class="example">
<a name="id347007"></a><p class="title"><b>Example 4.6. Variable scope examples</b></p>
<div class="example-contents"><pre class="programlisting">


// assign variable to Smarty object scope
$smarty-&gt;assign('foo','smarty');

// assign variables to data object scope
$data = $smarty-&gt;createData();
$data-&gt;assign('foo','data');
$data-&gt;assign('bar','bar-data');

// assign variables to other data object scope
$data2 = $smarty-&gt;createData($data);
$data2-&gt;assign('bar','bar-data2');

// assign variable to template object scope
$tpl = $smarty-&gt;createTemplate('index.tpl');
$tpl-&gt;assign('bar','bar-template');

// assign variable to template object scope with link to Smarty object
$tpl2 = $smarty-&gt;createTemplate('index.tpl',$smarty);
$tpl2-&gt;assign('bar','bar-template2');

// This display() does see $foo='smarty' from the $smarty object
$smarty-&gt;display('index.tpl');

// This display() does see $foo='data' and $bar='bar-data' from the data object $data
$smarty-&gt;display('index.tpl',$data);

// This display() does see $foo='data' from the data object $data 
// and $bar='bar-data2' from the data object $data2
$smarty-&gt;display('index.tpl',$data2);

// This display() does see $bar='bar-template' from the template object $tpl
$tpl-&gt;display();  // or $smarty-&gt;display($tpl);

// This display() does see $bar='bar-template2' from the template object $tpl2
// and $foo='smarty' form the Smarty object $foo
$tpl2-&gt;display();  // or $smarty-&gt;display($tpl2);

    </pre></div>
</div>
<p><br class="example-break">
   </p>
<p>
    See also <a class="link" href="api.assign.html" title="assign()"><code class="varname">assign()</code></a>,
    <a class="link" href="api.create.data.html" title="createData()"><code class="varname">createData()</code></a> and
    <a class="link" href="api.create.template.html" title="createTemplate()"><code class="varname">createTemplate()</code></a>.
</p>
</div>
<div class="navfooter">
<hr>
<table width="100%" summary="Navigation footer">
<tr>
<td width="40%" align="left">
<a accesskey="p" href="language.variables.html">Prev</a> </td>
<td width="20%" align="center"><a accesskey="u" href="language.variables.html">Up</a></td>
<td width="40%" align="right"> <a accesskey="n" href="language.config.variables.html">Next</a>
</td>
</tr>
<tr>
<td width="40%" align="left" valign="top">Chapter 4. Variables </td>
<td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td>
<td width="40%" align="right" valign="top"> Variables loaded from config files</td>
</tr>
</table>
</div>
</body>
</html>
